package com.davidniu.lesson03;

import com.davidniu.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestSelect {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();
            //使用?占位符代替参数
            String sql = "select * from users  where id =?;";
            stmt = conn.prepareStatement(sql);//预编译sql,先写sql,不执行
            //手动给参数赋值
            stmt.setInt(1, 1);

            //执行
            rs = stmt.executeQuery();
            while(rs.next()) {
                System.out.println("id = "+rs.getInt("id"));
                System.out.println("Name = "+rs.getString("NAME"));
                System.out.println("Password = "+rs.getString("PASSWORD"));
                System.out.println("Email = "+rs.getString("email"));
                System.out.println("Birthday = "+rs.getDate("birthday"));
                System.out.println("======================");
            }
        } catch(SQLException e){
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn, stmt, rs);
        }
    }
}
